

import {getToken} from "@/utils/index.js"

class fetchConfig {
    headers = {
        "Content-Type":"application/json;charset=utf-8",
    }
    constructor(baseUrl){
        this.baseUrl = baseUrl
    }
    get(url,params={}){
        return fetch(this.baseUrl+url,{
            method:"GET",
            body:JSON.stringify(params),
            headers:{
                ...this.headers,
                "Token":getToken()
            }
        }).then((res)=>{
            return res.json()
        })
    }
    post(url,params={},options){
        return fetch(this.baseUrl+url,{
            method:"POST",
            body: options?.type === 'formData' ? params : JSON.stringify(params),
            headers: options?.type === 'formData' ? {
                "Token":getToken()
            } : {
                ...this.headers,
                "Token":getToken()
            }
        }).then((res)=>{
            switch(url){
                case '/sendMessage':
                    return res
                case  '/downloadFile':
                    return res
                default:
                    return res.json()
            }
        })
    }
}


export default fetchConfig